//
//  CatagoryViewController.m
//  GiftSay
//
//  Created by dllo on 16/1/6.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "CatagoryViewController.h"
#import "HXTactikCustomCollectionCell.h"
#import "HXGiftCollectionCell.h"
#import "HXTactikFirstModel.h"
#import "HXCatagoryFirstModel.h"
#import "HXInsideAllProfessionViewController.h"
#import "CatagoryInsideViewController.h"
#import "HXGiftFirstModel.h"
#import "HotViewController.h"
#import "SearchArtifactViewController.h"
#import "Filters.h"
#define kTactikUrl @"http://api.liwushuo.com/v2/collections?limit=6&offset=0"
#define kTactikCatagory @"http://api.liwushuo.com/v2/channel_groups/all"

@interface CatagoryViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UISegmentedControl *segmentControl;
@property (nonatomic, strong) UICollectionView *catagoryCollection;
@property (nonatomic, strong) NSMutableArray *giftData;

@end

@implementation CatagoryViewController
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    //获取攻略 - 专题数据
    [self getData:kTactikUrl];
    //获取攻略 - 品类数据
    [self getData:kTactikCatagory];
    //获取礼物数据
    [self getData:kGiftData];
    //NavigationSeg
    self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"攻略", @"礼物"]];
    self.segmentControl.frame = CGRectMake(self.view.width / 4, 5, self.view.width / 2, 30);
    self.segmentControl.selectedSegmentIndex = 0;
    [self.navigationController.navigationBar addSubview:_segmentControl];
    [self.segmentControl addTarget:self action:@selector(segmentDidPress:) forControlEvents:UIControlEventValueChanged];
    //查看全部专题
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jump) name:@"AllProfession" object:nil];
    //scrollview跳转
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollJumpToDetail:) name:@"scrollJumpToDetail" object:nil];
    //全部专题item跳转
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemJumpToDetail:) name:@"JumpToInside" object:nil];
    //礼物item跳转
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JumpToHot:) name:@"JumpToHot" object:nil];
    //选礼神器 - hot
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JumpToHotFromGod:) name:@"XuanLiShenQi" object:nil];
}

- (void)search {
    [super search];
    self.segmentControl.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
            self.navigationController.navigationBar.barTintColor = kNightModeNavigationColor;
        } else {
            self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:248 / 255.0 green:78 / 255.0 blue:78 / 255.0 alpha:1];
        }
    self.segmentControl.hidden = NO;
}
//跳转 全部专题
- (void)jump {
    self.segmentControl.hidden = YES;
    
    self.allProfession = [[HXAllProfessionViewController alloc] init];
    
    
    [self.navigationController pushViewController:self.allProfession animated:NO];
    
}
//礼物item - Hotviewcontroller
- (void)JumpToHot:(NSNotification *)sender {
    HotViewController *item = [[HotViewController alloc] init];
    NSArray *arr = sender.object;
    self.segmentControl.hidden = YES;
    NSString *ids = arr.firstObject;

    item.ids= ids;
    item.name = arr.lastObject;
    [self.navigationController pushViewController:item animated:YES];
}
//scrollView跳转
- (void)scrollJumpToDetail:(NSNotification *)sender {
    self.segmentControl.hidden = YES;
    HXInsideAllProfessionViewController *insideAllProfessionViewController = [[HXInsideAllProfessionViewController alloc] init];
    
    insideAllProfessionViewController.model = sender.object;
    [self.navigationController pushViewController:insideAllProfessionViewController animated:NO];
}
//item跳转
- (void)itemJumpToDetail:(NSNotification *)sender {
    self.segmentControl.hidden = YES;
    CatagoryInsideViewController *inside = [[CatagoryInsideViewController alloc] init];
    inside.model = sender.object;
    [self.navigationController pushViewController:inside animated:NO];
    
}
//选礼神器 - hot
- (void)JumpToHotFromGod:(NSNotification *)sender {
    self.segmentControl.hidden = YES;

    SearchArtifactViewController *search = [[SearchArtifactViewController alloc] init];
    
    NSString *str = @"http://api.liwushuo.com/v2/search/item_filter";
    
    [LYKNetWorkTool getWithUrl:str parameter:nil httpHeader:nil resposeType:ResponseTypeDATA success:^(id result) {
        
        id any = [NSJSONSerialization JSONObjectWithData:result options:0 error:nil];
        
        NSDictionary *data = any[@"data"];
        NSArray *filters = data[@"filters"];
        NSMutableArray *temp = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in filters) {
            Filters *filters = [[Filters alloc] initWithDasource:dic];
            NSLog(@"%ld",filters.channel.count);
            [temp addObject:filters];
        }
        
        //                self.filter = self.hotChanal[0];
        search.array = [NSArray arrayWithArray:temp];
        [self.navigationController pushViewController:search animated:YES];
        //                [self.collection reloadData];
        //                [self.choseCollection reloadData];
    } fail:^(NSError *error) {
        
    }];
}


- (void)segmentDidPress:(UISegmentedControl *)sender {
//    NSLog(@"%ld", sender.selectedSegmentIndex);
    if (sender.selectedSegmentIndex == 0) {
        [self.catagoryCollection setContentOffset:CGPointMake(0, 0)];
    } else {
        [self.catagoryCollection setContentOffset:CGPointMake(self.view.width, 0)];
    }
}
//collectionview偏移 - segment偏移
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.catagoryCollection) {
        if (self.catagoryCollection.contentOffset.x + 0.5 < 1) {
            self.segmentControl.selectedSegmentIndex = 0;
        } else {
            self.segmentControl.selectedSegmentIndex = 1;
        }
    }
}
//seg对应collectionView
- (void)createView {
//    NSLog(@"qqqqqq%f", self.view.frame.size.);
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.width, self.view.height - 49);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    self.catagoryCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 49) collectionViewLayout:flowLayout];
    self.catagoryCollection.delegate = self;
    self.catagoryCollection.dataSource = self;
    [self.catagoryCollection registerClass:[HXTactikCustomCollectionCell class] forCellWithReuseIdentifier:@"CatogaryCollection"];
    [self.catagoryCollection registerClass:[HXGiftCollectionCell class] forCellWithReuseIdentifier:@"GiftCollection"];
    self.catagoryCollection.pagingEnabled = YES;
    self.catagoryCollection.showsHorizontalScrollIndicator = NO;
    self.catagoryCollection.bounces = NO;
    [self.view addSubview:_catagoryCollection];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    //攻略
    if (indexPath.item == 0) {
        
        HXTactikCustomCollectionCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"CatogaryCollection" forIndexPath:indexPath];
        //属性传值
        item.tactikProfessionArray = self.tactikArray;
        item.tactikCatagoryArray = self.tactikCatagoryArray;


        return item;
    } else {
        //礼物
        HXGiftCollectionCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"GiftCollection" forIndexPath:indexPath];
        item.giftData = self.giftData;
        
//        self.segmentControl.selectedSegmentIndex = 1;

        return item;
    }
    
}
//获取数据
- (void)getData:(NSString *)urlStr {
    [LYKNetWorkTool getWithUrl:urlStr parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id success) {
//        NSLog(@"%@", success);
   
        if ([urlStr isEqualToString: kTactikUrl]) {
            NSDictionary *dictionary = success[@"data"];
            NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
            for (NSDictionary *dic in dictionary[@"collections"]) {
                HXTactikFirstModel *tactikFirst = [[HXTactikFirstModel alloc] init];
                [tactikFirst setValuesForKeysWithDictionary:dic];
                [arr addObject:tactikFirst];
            }
            self.tactikArray = [NSMutableArray arrayWithArray:arr];
        }
        
        //攻略--品类。
        if ([urlStr isEqualToString:kTactikCatagory]) {
            NSDictionary *dictionary = success[@"data"];
            NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
            for (NSDictionary *dic in dictionary[@"channel_groups"]) {
                HXCatagoryFirstModel *firstModel = [[HXCatagoryFirstModel alloc] init];
                
                [firstModel setValuesForKeysWithDictionary:dic];
                [arr addObject:firstModel];
            }
            self.tactikCatagoryArray = [NSArray arrayWithArray:arr];
        }
        
        //礼物数据
        if ([urlStr isEqualToString:kGiftData]) {
            NSDictionary *dic = success[@"data"];
            NSArray *array = dic[@"categories"];
            NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
            for (NSDictionary *dictionary in array) {
                HXGiftFirstModel *model = [[HXGiftFirstModel alloc] init];
                [model setValuesForKeysWithDictionary:dictionary];
                [arr addObject:model];
            }
            self.giftData = [NSMutableArray arrayWithArray:arr];
        }
        if (self.tactikArray > 0 && self.tactikCatagoryArray > 0) {
            [self createView];
        }
        
 
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
